<template>
  <div class="app-container">
    <top-simple-search @searchFun="query" @addFun="toAdd">
      <el-form-item label="" prop="gender">
        <gender-form-item  v-model="formData.gender" clearable></gender-form-item>
      </el-form-item>
      <el-form-item label="" prop="is_active">
        <el-select v-model="formData.is_active" placeholder="请选择是否在职" clearable>
          <el-option label="在职" :value="1"></el-option>
          <el-option label="离职" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-button
        slot="other-button"
        type="primary"
        size="small"
        plain  @click="toTongbu" v-has-permi="['tongbu']">同步</el-button>
    </top-simple-search>
    <simple-table ref="myTable" size="mini" >
      <my-title-column align="left" prop="username" min-width="150" label="用户名" @click="toDetail"></my-title-column>
      <el-table-column prop="alias" label="姓名"/>
      <el-table-column prop="identity" label="工号" width="100"/>
      <el-table-column prop="phone" label="电话" width="150"/>
      <el-table-column prop="dept_info" label="部门" show-overflow-tooltip min-width="150"/>
      <el-table-column prop="role_info" label="角色" min-width="150" show-tooltip-when-overflow/>
      <el-table-column align="left" label="状态" width="60">
        <template slot-scope="{ row }">
          <span v-if="row.is_active === true">在职</span>
          <span v-if="row.is_active === false">离职</span>
        </template>
      </el-table-column>
      <my-time-column prop="last_login" label="最后登录时间"/>
      <el-table-column align="left" label="操作" width="260px" fixed="right">
        <template slot-scope="{row}">
          <detail-button @click="toDetail(row)"></detail-button>
          <edit-button @click="toEdit(row)"></edit-button>
          <my-button @click="resetPassword(row.id)"  handle="chongzhimima">重置</my-button>
          <my-button @click="toShowRoleList(row)"  handle="chakanjuese">角色</my-button>
          <my-button @click="toUnlock(row.id)"  handle="unlock">解锁</my-button>
          <delete-button @click="toDelete(row.id)"></delete-button>
        </template>
      </el-table-column>
    </simple-table>
    <detail-dialog v-if="visible" :visible.sync="visible" :row="currRow" @reloadList="getList" :disabled="disabled"></detail-dialog>
    <role-list-dialog :id="currRow.id" :visible.sync="roleVisible" v-if="roleVisible" ></role-list-dialog>
  </div>
</template>
<script>
  import simpleTable from '@/mixins/SimpleTable'
  import DetailDialog from './DetailDialog'
  import MyTitleColumn from '@/components/TableColumn/MyTitleColumn'
  import MyTimeColumn from '@/components/TableColumn/MyTimeColumn'
  import MySwitchColumn from '@/components/TableColumn/MySwitchColumn'
  import user from "@/api/systemManagement/user/index";
  import RoleListDialog from './RoleListDialog'
  import GenderFormItem from '../../../../components/FormItem/GenderFormItem'
  // 获取登录用户信息
  export default {
    name:"userTable",
    components: { GenderFormItem, RoleListDialog, MySwitchColumn, MyTimeColumn, MyTitleColumn, DetailDialog },
    mixins: [simpleTable],
    data() {
      return {
        formData:{gender:undefined,is_active:undefined,search_value:undefined},
        roleUsersVisible: false,
        roleVisible:false
      }
    },
    methods: {
      toTongbu(){
        user.usersTongbu().then(res=>{
          this.msgSuccess(res.msg);
        })
      },
      toShowRoleList(row) {
        this.currRow = row;
        this.roleVisible = true;
      },
      showUserListDialog(row) {
        this.roleUsersVisible = true
        this.currRow = row
      },
      showDataScopeDialog(row) {
        this.$router.push({ path: 'roleAuthManagement', query: { id: row.id } })
      },
      resetPassword(id) {
        this.$confirm("此操作将重置该用户的密码, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          user.resetUserPassword(id).then(res => {
            this.msgSuccess(res.msg);
          });
        });
      },
      toUnlock(id){
        this.$confirm("登录解锁, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          user.accountsUnlock(id).then(res => {
            this.msgSuccess(res.msg);
          });
        });
      },
      query(queryData) {
        if(typeof queryData == "object"){
          this.formData = {...this.formData,...queryData}
        }else{
          this.formData.search_value = queryData;
        }
        this.formData.dept=undefined
        this.$refs["myTable"].searchList(this.formData);
      },
    }
  }
</script>
<style lang="scss" scoped>
  .app-container{
    padding-top:15px;
  }
  </style>
